package com.xiaoxi.exam.utils;

import cn.xinzhili.xutils.core.util.HashUtils;
import com.xiaoxi.exam.bean.message.MessageInfo;
import com.xiaoxi.exam.bean.response.MessageInfoListResponse;
import com.xiaoxi.exam.model.Message;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.springframework.beans.BeanUtils;

public class MessageFactory {

  public static MessageInfoListResponse toMessageInfoListResponse(List<Message> messages) {
    MessageInfoListResponse response = new MessageInfoListResponse();
    response.setMessageInfos(
        getMessageInfoList(messages));
    return response;
  }

  private static List<MessageInfo> getMessageInfoList(List<Message> messages) {
    return messages.stream().map(MessageFactory::getMessageInfo).collect(Collectors.toList());
  }

  public static MessageInfo getMessageInfo(Message m) {
    MessageInfo messageInfo = new MessageInfo();
    if (Objects.isNull(m)) {
      return messageInfo;
    }
    BeanUtils.copyProperties(m, messageInfo);
    messageInfo.setId(HashUtils.encode(m.getId()));
    DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
    messageInfo.setCreatedAt(m.getCreatedAt().format(df));
    return messageInfo;
  }
}
